package com.minimalist.basic.mapper;

import com.minimalist.basic.entity.po.MOperatorLog;
import com.minimalist.basic.entity.po.MUser;
import com.minimalist.basic.entity.po.table.MOperatorLogTableDef;
import com.minimalist.basic.entity.po.table.MUserTableDef;
import com.minimalist.basic.entity.vo.operatorlog.OperatorLogQueryVO;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

import java.util.List;

/**
 * 操作日志表 映射层。
 *
 * @author 11987
 * @since 2025-01-23
 */
public interface MOperatorLogMapper extends BaseMapper<MOperatorLog> {

    /**
     * 分页查询操作日志列表
     *
     * @param queryVO 查询条件
     * @return 分页数据
     */
    default Page<MOperatorLog> selectPageOperatorLogList(OperatorLogQueryVO queryVO) {
        return paginate(queryVO.getPageNum(), queryVO.getPageSize(),
                QueryWrapper.create()
                        .eq(MOperatorLog::getStatus, queryVO.getStatus())
                        .like(MOperatorLog::getName, queryVO.getName())
                        .like(MOperatorLog::getMethod, queryVO.getMethod())
                        .in(MOperatorLog::getCreateId, QueryWrapper.create()
                                .select(MUserTableDef.MUSER.USER_ID)
                                .from(MUserTableDef.MUSER)
                                .like(MUser::getUsername, queryVO.getCreatorName()))
                        .orderBy(MOperatorLogTableDef.MOPERATOR_LOG.REQUEST_TIME
                                .desc().nullsLast())
        );
    }

    /**
     * 根据操作日志ID查询操作日志
     *
     * @param operatorLogId 操作日志ID
     * @return 操作日志
     */
    default MOperatorLog selectOperatorLogByOperatorLogId(Long operatorLogId) {
        return selectOneByQuery(QueryWrapper.create()
                .eq(MOperatorLog::getOperatorLogId, operatorLogId));
    }

    /**
     * 根据操作日志ID删除操作日志
     *
     * @param operatorLogId 操作日志ID
     * @return 影响行数
     */
    default int deleteOperatorLogByOperatorLogId(Long operatorLogId) {
        return deleteByQuery(QueryWrapper.create()
                .eq(MOperatorLog::getOperatorLogId, operatorLogId));
    }

    /**
     * 根据操作日志ID批量删除操作日志
     *
     * @param operatorLogIds 操作日志ID列表
     */
    default void deleteUserByUserIds(List<Long> operatorLogIds) {
        deleteByQuery(QueryWrapper.create()
                .in(MOperatorLog::getOperatorLogId, operatorLogIds));
    }
}
